/**
 * file
 */
package com.tdkj.action.backstage.admin;

import java.sql.Timestamp;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import com.tdkj.action.BaseAction;
import com.tdkj.model.financial.BeanFinancialAccount;
import com.tdkj.model.user.BeanAdminIntoSilverRecords;
import com.tdkj.model.user.BeanMessage;
import com.tdkj.model.user.BeanUser;

/**
 * 
 * FileName: AdminIntoSilverActiom.java
 * 
 * @Description: TODO管理员发放银币操作Aciton
 * 
 *               All rights Reserved, Designed By ZTE-ITS
 * 
 *               Copyright: Copyright(C) 2010-2011
 * 
 *               Company 天成科技有限公司
 * 
 * @author: 李兴建
 * 
 * @version V1.0
 * 
 *          Createdate: 2012-8-6 下午04:36:33
 * 
 * 
 * 
 *          Modification History:
 * 
 *          Date Author Version Discription
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 *          
 *          ----------------------------------------------------------------------
 *          -------------
 * 
 *          2012-8-6 李兴建 1.0 1.0
 * 
 *          Why & What is modified: <修改原因描述>
 */
public class AdminIntoSilverActiom extends BaseAction {
	private static final long serialVersionUID = -467454042036682187L;

	private String userName;
	private int silverCount;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		Criteria criteria = this.createCriteria(BeanAdminIntoSilverRecords.class);
		criteria.addOrder(Order.desc("dataTime"));// 对状态进行排序
		this.queryByPage(criteria, "adminIntoSilverActiom!findList.action", 10);
		return super.execute();
	}

	public String findList() {
		Criteria criteria = this.createCriteria(BeanAdminIntoSilverRecords.class);
		criteria.addOrder(Order.desc("dataTime"));// 对状态进行排序
		this.queryByPage(criteria, "adminIntoSilverActiom!findList.action", 10);
		return SUCCESS;
	}

	/**
	 * 发放银币操作
	 * 
	 * @return
	 * @throws Exception
	 */
	public String intoSilver() throws Exception {
		List<BeanUser> list = null;
		BeanUser userTemp = null;
		list = this.getBaseService().findByProperty("username", userName, BeanUser.class);
		if (list.size() < 1) {
			this.addFieldError("userNameErrorVlidate", "没有找到你要操作的用户");
			return execute();
		} else if (list.size() > 1) {
			this.addFieldError("userNameErrorVlidate", "你操作的账号异常，找到1个以上用户");
			return execute();
		}
		if (silverCount < 1) {
			this.addFieldError("silverCountErrorVlidate", "银币至少一个");
			return execute();
		}

		userTemp = list.get(0);

		BeanFinancialAccount beanAccount = userTemp.getFinancialAccount();
		beanAccount.setSilver(beanAccount.getSilver() + silverCount);
		this.getBaseService().saveOrUpdate(beanAccount, BeanFinancialAccount.class);
		this.addFieldError("infoError", "操作成功，放入 ：" + userTemp.getUsername() + " 账号 " + silverCount + "个银币");

		BeanAdminIntoSilverRecords beanAdminIntoSilverRecords = new BeanAdminIntoSilverRecords();
		beanAdminIntoSilverRecords.setBeanAdmin(getCurrentAdmin());
		beanAdminIntoSilverRecords.setDataTime(new Timestamp(System.currentTimeMillis()));
		beanAdminIntoSilverRecords.setUser(userTemp);
		beanAdminIntoSilverRecords.setSilverCount(silverCount);
		this.getBaseService().save(beanAdminIntoSilverRecords);

		BeanMessage beanMessage = new BeanMessage();
		beanMessage.setCreatetime(new Timestamp(System.currentTimeMillis()));
		//beanMessage.setTouserid(userTemp.getId());
		beanMessage.setReciver(userTemp);
		beanMessage.setTitle("管理员发放银币");
		//beanMessage.setFromuserid(this.getCurrentAdmin().getId());
		beanMessage.setBeanAdmin(this.getCurrentAdmin());
		//beanMessage.setSender(sender)
		beanMessage.setMsgtype(0);
		//beanMessage.setFromType(1);
		beanMessage.setContent("【"+this.getCurrentAdmin().getAdminname()+"】给你发放  "+silverCount+"  银币,请你查收");
		this.getBaseService().save(beanMessage);
		
		userName="";
		silverCount=0;
		return execute();
	}

	/**
	 * @return the userName
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * @param userName
	 *            the userName to set
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	/**
	 * @return the silverCount
	 */
	public int getSilverCount() {
		return silverCount;
	}

	/**
	 * @param silverCount
	 *            the silverCount to set
	 */
	public void setSilverCount(int silverCount) {
		this.silverCount = silverCount;
	}

}
